import {useEffect, useRef} from 'react';
import * as echarts from 'echarts'; //全局引入 ，可按需引入
import zhejiang from './json/china';

const randomData = () => {
    return Math.round(Math.random() * 100);
};

const Map: React.FC = (props: any) => {
    const {teamType, mapData, onMapClick} = props;
    const chartRef = useRef(null);
    let myChart: any = null;
    let chinaDatas: any = [];

    const loadingChina = () => {
        echarts.registerMap('china', zhejiang);
        const chart = echarts.getInstanceByDom(chartRef.current);
        if (chart) {
            myChart = chart;
        } else {
            myChart = echarts.init(chartRef.current);
        }
        mapOption('莆田', chinaDatas, mapData); //初始化-创建中国地图
    };
    let chinaGeoCoordMap = {
        北京: [116.4551, 40.2539],
        黑龙江: [127.9688, 45.368],
        内蒙古: [110.3467, 41.4899],
        吉林: [125.8154, 44.2584],
        宜宾市: [104.630825, 28.760189],
        辽宁: [123.1238, 42.1216],
        河北: [114.4995, 38.1006],
        天津: [117.4219, 39.4189],
        山西: [112.3352, 37.9413],
        陕西: [109.1162, 34.2004],
        甘肃: [103.5901, 36.3043],
        宁夏: [106.3586, 38.1775],
        青海: [101.4038, 36.8207],
        新疆: [87.9236, 43.5883],
        西藏: [91.11, 29.97],
        四川: [103.9526, 30.7617],
        重庆: [108.384366, 30.439702],
        山东: [117.1582, 36.8701],
        河南: [113.4668, 34.6234],
        江苏: [118.8062, 31.9208],
        安徽: [117.29, 32.0581],
        湖北: [114.3896, 30.6628],
        浙江: [119.5313, 29.8773],
        福建: [119.4543, 25.9222],
        莆田: [119.4543, 25.9222],
        江西: [116.0046, 28.6633],
        湖南: [113.0823, 28.2568],
        贵州: [106.6992, 26.7682],
        云南: [102.9199, 25.4663],
        广东: [113.12244, 23.009505],
        广西: [108.479, 23.1152],
        海南: [110.3893, 19.8516],
        上海: [121.4648, 31.2891],
        '浙江,宁波': [102.712251, 25.040609],
    };

    useEffect(() => {
        mapData.map((item: any) => {
            chinaDatas.push([item]);
            chinaGeoCoordMap[item.name] = item.lonlat;
        });
        loadingChina();
        return () => {
            myChart && myChart.dispose();
        };
    }, [mapData]);

    // 定义中心点和起始点，代码如下：

    const convertData = (data: any) => {
        let res = [];
        //组团1,接待2
        if (teamType == 1) {
            for (var i = 0; i < data.length; i++) {
                var dataItem = data[i];
                var fromCoord = chinaGeoCoordMap[dataItem[0].name];
                var toCoord = chinaGeoCoordMap['莆田']; //中心点地理坐标
                if (fromCoord && toCoord) {
                    res.push([
                        {
                            coord: toCoord,
                        },
                        {
                            coord: fromCoord,
                            value: data[0].value,
                        },
                    ]);
                } else {
                    res.push([
                        {
                            coord: toCoord,
                        },
                        {
                            coord: data[0].lonlat,
                            value: data[0].value,
                        },
                    ]);
                }
            }
        } else {
            for (var i = 0; i < data.length; i++) {
                var dataItem = data[i];
                var fromCoord = chinaGeoCoordMap[dataItem[0].name];
                var toCoord = chinaGeoCoordMap['莆田']; //中心点地理坐标
                if (fromCoord && toCoord) {
                    res.push([
                        {
                            coord: fromCoord,
                            value: dataItem[0].value,
                        },
                        {
                            coord: toCoord,
                        },
                    ]);
                } else {
                    res.push([
                        {
                            coord: data[0].lonlat,
                            value: data[0].value,
                        },
                        {
                            coord: toCoord,
                        },
                    ]);
                }
            }
        }

        return res;
    };

    var series: any = [];

    const mapOption = (zoneName: string, chinaDatas: any, mapData: any) => {
        [[zoneName, chinaDatas]].forEach(function (item, i) {
            series.push(
                {
                    type: 'lines',
                    zlevel: 2,
                    effect: {
                        show: true,
                        period: 4, //箭头指向速度，值越小速度越快
                        trailLength: 0.02, //特效尾迹长度[0,1]值越大，尾迹越长重
                        symbol: 'arrow', //箭头图标
                        symbolSize: 3, //图标大小
                    },
                    lineStyle: {
                        width: 0.5, //尾迹线条宽度
                        opacity: 0.11, //尾迹线条透明度
                        curveness: 0.3, //尾迹线条曲直度
                        color: '#25CCFD',
                    },
                    data: convertData(item[1]),
                },
                {
                    //涟漪
                    type: 'effectScatter',
                    coordinateSystem: 'geo',
                    zlevel: 2,
                    rippleEffect: {
                        //涟漪特效
                        period: 4, //动画时间，值越小速度越快
                        brushType: 'stroke', //波纹绘制方式 stroke, fill
                        scale: 4, //波纹圆环最大限制，值越大波纹越大
                    },
                    label: {
                        show: true,
                        position: 'right', //显示位置
                        offset: [5, 0], //偏移设置
                        formatter: function (params) {
                            //圆环显示文字
                            return params.data.name;
                        },
                        fontSize: 13,
                        emphasis: {
                            show: true,
                            formatter: function (params) {
                                //圆环显示文字
                                return params.data.name;
                            },
                        },
                    },
                    symbol: 'circle',
                    symbolSize: 5,
                    // symbolSize: function (val) {
                    //   return 5 + val[2] * 5; //圆环大小
                    // },
                    itemStyle: {
                        show: false,
                        color: '#25CCFD',
                    },
                    data: item[1].map(function (dataItem) {
                        if (chinaGeoCoordMap[dataItem[0].name]) {
                            return {
                                //在这里定义你所要展示的数据
                                name: dataItem[0].name,
                                // value:1
                                value: chinaGeoCoordMap[dataItem[0].name].concat([
                                    dataItem[0].value,
                                ]),
                            };
                        }
                    }),
                },

                //中心点
                {
                    type: 'scatter',
                    coordinateSystem: 'geo',
                    zlevel: 2,
                    rippleEffect: {
                        period: 4,
                        brushType: 'stroke',
                        scale: 4,
                    },

                    label: {
                        show: true,
                        position: 'right',
                        //offset:[5, 0],
                        color: '#0f0',
                        formatter: '{b}',
                        textStyle: {
                            color: '#25CCFD',
                        },
                        emphasis: {
                            show: true,
                            color: '#f60',
                        },
                    },

                    symbol: 'pin',
                    symbolSize: 50,
                    data: [
                        {
                            name: item[0],
                            value: chinaGeoCoordMap[item[0]].concat([10]),
                        },
                    ],
                },
                {
                    type: 'map',
                    mapType: 'china',
                    geoIndex: 0,
                    label: {
                        show: true,
                        emphasis: {
                            show: true,
                        },
                    },
                    data: mapData || [],
                },
            );
        });

        let option = {
            tooltip: {
                trigger: 'item',
                backgroundColor: 'rgba(0, 0, 0, 0.6)',
                borderColor: '#25CCFD',
                showDelay: 0,
                hideDelay: 0,
                enterable: true,
                transitionDuration: 0,
                extraCssText: 'z-index:100',
                formatter: function (params: any, ticket, callback) {
                    //根据业务自己拓展要显示的内容
                    var res = '';
                    let name = params.name;
                    var value = params.value;
                    if (params.componentSubType == 'map') {
                        mapData.map((item: any) => {
                            if (params.name.indexOf(item.name) != -1) {
                                res =
                                    teamType == 1
                                        ? "<span style='color:#fff;'>莆田 → 前往 →" +
                                        name +
                                        "</span><br/><div style='color:#fff;'>团数数量:" +
                                        item.value +
                                        '团</div><div style="color:#fff;">游客数量:' +
                                        item.value1 +
                                        '人次</div>'
                                        : "<span style='color:#fff;'>" +
                                        name +
                                        "→ 前往 → 莆田 </span><br/><div style='color:#fff;'>团数数量:" +
                                        item.value +
                                        '团</div><div style="color:#fff;">游客数量:' +
                                        item.value1 +
                                        '人次</div>';
                            }
                        });
                        return res;
                    } else if (params.componentSubType == 'lines') {
                    } else {
                        mapData.map((item: any) => {
                            if (params.name.indexOf(item.name) != -1) {
                                res =
                                    teamType == 1
                                        ? "<span style='color:#fff;'>莆田 → 前往 →" +
                                        name +
                                        "</span><br/><div style='color:#fff;'>团数数量:" +
                                        item.value +
                                        '团</div><div style="color:#fff;">游客数量:' +
                                        item.value1 +
                                        '人次</div>'
                                        : "<span style='color:#fff;'>" +
                                        name +
                                        "→ 前往 → 莆田 </span><br/><div style='color:#fff;'>团数数量:" +
                                        item.value +
                                        '团</div><div style="color:#fff;">游客数量:' +
                                        item.value1 +
                                        '人次</div>';
                            }
                        });
                        return res;
                    }
                },
            },
            // visualMap: {
            //   min: 0,
            //   max: 100,
            //   text: ['High', 'Low'],
            //   realtime: false,
            //   calculable: true,
            //   inRange: {
            //     color: ['lightskyblue', 'yellow', 'orangered']
            //   }
            // },
            geo: {
                map: 'china',
                zoom: 1.09,
                top: 300,
                aspectScale: 0.77, //长宽比
                left: '29%',
                label: {
                    emphasis: {
                        show: false,
                    },
                },
                roam: false, //是否允许缩放
                itemStyle: {
                    color: 'rgba(37,204,253,0.1)', //地图背景色
                    borderColor: '#fff', //省市边界线00fcff 516a89
                    borderWidth: 1,
                    emphasis: {
                        color: 'rgba(37, 43, 61, .5)', //悬浮背景
                    },
                },
            },
            series: series,
        };

        myChart.setOption(option); //绘图
        myChart.on('click', function (e: any) {
            mapData.map((item: any) => {
                if (e.name.indexOf(item.name) != -1) {
                    onMapClick(item);
                }
            });
        });
        window.addEventListener('resize', () => {
            if (myChart && myChart.resize) {
                myChart.resize();
            }
        });
    };

    const echartsMapClick = () => {
        //点击地图模块逻辑事件
    };
    return (
        <div
            id="map"
            ref={chartRef}
            style={{width: '100%', height: '100%'}}
        ></div>
    );
};
export default Map;
